package io.gitee.chearnee.fw.datasecurity.core.processor;

import io.gitee.chearnee.fw.datasecurity.core.executor.desensitization.SDExecutorFactory;
import io.gitee.chearnee.fw.datasecurity.core.executor.desensitization.StringSDExecutor;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;

import java.util.Set;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 11/9/2022 4:40 PM
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SDResponseBodyProcessor {

    private static final StringSDExecutor stringSDExecutor = new StringSDExecutor();

    public static Object execute(Object body, MethodParameter returnType, Set<Class<?>> disabledClasses) {
        if (body == null) {
            return body;
        }
        if (returnType.getGenericParameterType().equals(String.class) && body instanceof String) {
            return stringSDExecutor.execute(body.toString(), returnType, disabledClasses);
        }
        return SDExecutorFactory.getSDExecutor(body).executeObject(body, null, disabledClasses);
    }
}
